package com.gs.wzmanager.controller;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.gs.wzmanager.dao.ForcastReviseDao;
import com.gs.wzmanager.entity.DSBean;
import com.gs.wzmanager.entity.ForcastRevise;
import com.gs.wzmanager.service.ForeService;
import com.gs.wzmanager.utils.HttpUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: Legolas
 * Date: 2017/11/2 0002
 */
@Controller
@RequestMapping("/revise")
public class ForcastReviseController {

    Gson gson = new GsonBuilder().create();
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    ForcastReviseDao dao;

    @Autowired
    ForeService foreService;

    @RequestMapping(value = "/listView")
    public String view(){
        return "revise/list";
    }

    @RequestMapping("/listThree")
    @ResponseBody
    public List<ForcastRevise> getThree() {
        return dao.queryCYReviseThree();
    }

    @RequestMapping("/listWeek")
    @ResponseBody
    public List<ForcastRevise> getWeek() {
        return dao.queryCYReviseWeek();
    }

    @RequestMapping("/updateThree")
    @ResponseBody
    public ResponseEntity updateThree(@RequestBody ForcastRevise revise) {
        revise.setChecked(true);
        dao.updateReviseThree(revise);
        return HttpUtils.buildStringResponse("OK", HttpStatus.OK);
    }

    @RequestMapping("/updateWeek")
    @ResponseBody
    public ResponseEntity updateWeek(@RequestBody ForcastRevise revise) {
        revise.setChecked(true);
        dao.updateReviseWeek(revise);
        return HttpUtils.buildStringResponse("OK", HttpStatus.OK);
    }

    @RequestMapping("/check")
    public String checkView(){
        return "forcastcheck";
    }


    @RequestMapping(value = "queryPortFore")
    @ResponseBody
    public List<DSBean> queryPortFore(String station) {
        logger.info("queryPortFore->{}",station);
        return foreService.queryPortFore(station);
    }

    @RequestMapping(value = "queryChannelFore")
    @ResponseBody
    public List<DSBean> queryChannelFore(String station) {
        logger.info("queryChannelFore->{}",station);
        return foreService.queryChannelFore(station);
    }

    @RequestMapping(value = "addPortFore")
    public ResponseEntity savePortFore(@RequestBody HashMap map){
        List<DSBean> dsBeans = gson.fromJson(map.get("dsBeans").toString(), new TypeToken<List<DSBean>>(){}.getType());
        String stationName = map.get("stationName").toString();
        return foreService.addPortFore(dsBeans,stationName);
    }

    @RequestMapping(value = "addChannelFore")
    public ResponseEntity saveChannelFore(@RequestBody HashMap map){
<<<<<<< HEAD
        logger.info("map=dsBeans->{}",map.get("dsBeans"));
        logger.info("map=stationName->{}",map.get("stationName"));
        return null;
        //return foreService.addChannelFore(dsBeans);
=======
        List<DSBean> dsBeans = gson.fromJson(map.get("dsBeans").toString(), new TypeToken<List<DSBean>>(){}.getType());
        String stationName = map.get("stationName").toString();
        return foreService.addChannelFore(dsBeans,stationName);
>>>>>>> 6847e0458df0c5f7e8899f7beb1cd210abc04252
    }

    @RequestMapping(value = "updatePortFore")
    public ResponseEntity updatePortFore(@RequestBody HashMap map){
        List<DSBean> dsBeans = gson.fromJson(map.get("dsBeans").toString(), new TypeToken<List<DSBean>>(){}.getType());
        String stationName = map.get("stationName").toString();
        return foreService.updatePortFore(dsBeans,stationName);
    }

    @RequestMapping(value = "updateChannelFore")
    public ResponseEntity updateChannelFore(@RequestBody HashMap map){
<<<<<<< HEAD
        logger.info("map=dsBeans->{}",map.get("dsBeans"));
        logger.info("map=stationName->{}",map.get("stationName"));
        return null;
        //return foreService.updateChannelFore(dsBeans);
=======
        List<DSBean> dsBeans = gson.fromJson(map.get("dsBeans").toString(), new TypeToken<List<DSBean>>(){}.getType());
        String stationName = map.get("stationName").toString();
        return foreService.updateChannelFore(dsBeans,stationName);
>>>>>>> 6847e0458df0c5f7e8899f7beb1cd210abc04252
    }

}
